<?php
class NavigationManagerController extends Zend_Controller_Action
{
    public function init ()
    {
        $uri = $this->_request->getPathInfo();
        $activeNav = $this->view->navigation()->findByHref($uri);
        if ($activeNav != null) {
            $activeNav->active = true;
            $activeNav->setClass("active");
        }
    }
    public function indexAction ()
    {
        $this->_redirect("/");
    }
    public function getsearchpageAction ()
    {
        $isError = $this->getRequest()->getParam("error");
        if ($isError) {
            $message = $this->getRequest()->getParam("message");
            $this->view->message = $message;
        }
    }
    public function searchitemAction ()
    {
        if (! key_exists("itemName", $_POST) || $_POST["itemName"] == null) {
            $this->_setParam("error", true);
            $this->_setParam("message", "Error: The keyword can not be empty!");
            $this->_forward("getsearchpage", "NavigationManager");
            return;
        } else {
            $itemName = $_POST["itemName"];
            $searNs = new Zend_Session_Namespace("keywordNs");
            $searNs->keyword = $itemName;
            $searchtype = $_POST["searchType"];
            switch ($searchtype) {
                case "term":
                    $this->_redirect("/NavigationManager/getsimilarobjects");
                    break;
                case "subject":
                	$this->_redirect("/NavigationManager/getsubjects");
                    break;
                default:
                    $this->_redirect("/");
                    break;
            }
        }
    }
    public function getsimilarobjectsAction ()
    {
        $page = $this->_getParam('page', 1);
        $searNs = new Zend_Session_Namespace("keywordNs");
        $itemName = $searNs->keyword;
        if ($itemName == null) {
            $this->_redirect("/");
        }
        $options = Zend_Registry::get("sysOptions");
        $pagesize = $options["pagesize"];
        $start = $pagesize * ($page - 1);
        $webOptions = Zend_Registry::get("webOptions");
        $restOpt = $webOptions["REST"];
        $collectionPath = $restOpt["term"];
        $parameters = array("order_by" => "term", "term_in" => $itemName, 
        "start" => $start, "limit" => $pagesize);
        $data = Application_Model_RESTWSManager::getData($collectionPath, 
        $parameters);
        $total = $data["total"];
        if ($total > 0) {
            $paginator = Zend_Paginator::factory(1 * $total);
            $paginator->setItemCountPerPage($pagesize);
            $paginator->setCurrentPageNumber($page);
            $this->view->paginator = $paginator;
            $this->view->terms = $total > 1 ? $data["terms"]["term"] : $data["terms"];
            $this->view->total = $total;
        }
        $viewStore=new Application_Model_ViewStore("NavigationManager", "getsimilarobjects",null,$page);
        $viewNs=new Zend_Session_Namespace("viewNs");
        $viewNs->levelOneView=$viewStore;
    }
    public function getsubjectsAction ()
    {
        $page = $this->_getParam('page', 1);
        $searNs = new Zend_Session_Namespace("keywordNs");
        $itemName = $searNs->keyword;
        if ($itemName == null) {
            $this->_redirect("/");
        }
        $options = Zend_Registry::get("sysOptions");
        $pagesize = $options["pagesize"];
        $start = $pagesize * ($page - 1);
        $webOptions = Zend_Registry::get("webOptions");
        $restOpt = $webOptions["REST"];
        $collectionPath = $restOpt["subject"];
        $parameters = array("order_by" => "name", "name_in" => $itemName, 
        "start" => $start, "limit" => $pagesize);
        $data = Application_Model_RESTWSManager::getData($collectionPath, 
        $parameters);
        $total = $data["total"];
        if ($total > 0) {
            $paginator = Zend_Paginator::factory(1 * $total);
            $paginator->setItemCountPerPage($pagesize);
            $paginator->setCurrentPageNumber($page);
            $this->view->paginator = $paginator;
            $this->view->subjects = $total > 1 ? $data["subjects"]["subject"] : $data["subjects"];
            $this->view->total = $total;
        }
        $viewStore=new Application_Model_ViewStore("NavigationManager", "getsubjects",null,$page);
        $viewNs=new Zend_Session_Namespace("viewNs");
        $viewNs->levelOneView=$viewStore;
    }
    
    public function getcategoriesAction ()
    {
        $page = $this->_getParam('page', 1);
        $options = Zend_Registry::get("sysOptions");
        $pagesize = $options["pagesize"];
        $start = $pagesize * ($page - 1);
        $webOptions = Zend_Registry::get("webOptions");
        $restOpt = $webOptions["REST"];
        $collectionPath = $restOpt["category"];
        $parameters = array("order_by" => "name", "start" => $start, 
        "limit" => $pagesize);
        $data = Application_Model_RESTWSManager::getData($collectionPath, 
        $parameters);
        $total = $data["total"];
        if ($total > 0) {
            $paginator = Zend_Paginator::factory(1 * $total);
            $paginator->setItemCountPerPage($pagesize);
            $paginator->setCurrentPageNumber($page);
            $this->view->paginator = $paginator;
            $this->view->cateogries = $total > 1 ? $data["categories"]["category"] : $data["categories"];
            $this->view->total = $total;
        }
        $viewStore=new Application_Model_ViewStore("NavigationManager", "getcategories",null,$page);
        $viewNs=new Zend_Session_Namespace("viewNs");
        $viewNs->levelOneView=$viewStore;
    }
    public function getcollectionsAction ()
    {
        $page = $this->_getParam('page', 1);
        $options = Zend_Registry::get("sysOptions");
        $pagesize = $options["pagesize"];
        $start = $pagesize * ($page - 1);
        $webOptions = Zend_Registry::get("webOptions");
        $restOpt = $webOptions["REST"];
        $collectionPath = $restOpt["collection"];
        $parameters = array("order_by" => "name", "start" => $start, 
        "limit" => $pagesize);
        $data = Application_Model_RESTWSManager::getData($collectionPath, 
        $parameters);
        $total = $data["total"];
        if ($total > 0) {
            $page = $this->_getParam('page', 1);
            $paginator = Zend_Paginator::factory(1 * $total);
            $paginator->setItemCountPerPage($pagesize);
            $paginator->setCurrentPageNumber($page);
            $this->view->paginator = $paginator;
            $this->view->collections = $total > 1 ? $data["collections"]["collection"] : $data["collections"];
            $this->view->total = $total;
        }
        $viewStore=new Application_Model_ViewStore("NavigationManager", "getcollections",null,$page);
        $viewNs=new Zend_Session_Namespace("viewNs");
        $viewNs->levelOneView=$viewStore;
    }
    public function getthemesAction ()
    {
        $page = $this->_getParam('page', 1);
        $options = Zend_Registry::get("sysOptions");
        $pagesize = $options["pagesize"];
        $start = $pagesize * ($page - 1);
        $webOptions = Zend_Registry::get("webOptions");
        $restOpt = $webOptions["REST"];
        $collectionPath = $restOpt["theme"];
        $parameters = array("order_by" => "title", "start" => $start, 
        "limit" => $pagesize);
        $data = Application_Model_RESTWSManager::getData($collectionPath, 
        $parameters);
        $total = $data["total"];
        $themes = $total > 1 ? $data["themes"]["theme"] : $data["themes"];
        foreach ($themes as &$theme) {
            if ($theme["num_multimedia"] > 0) {
                $mediaPath = $theme['multimedia_uri'];
                $mediaPathArr = explode("?", $mediaPath);
                $mediaPath = $mediaPathArr[0];
                $mediaData = Application_Model_RESTWSManager::getData(
                $mediaPath);
                $mediaTotal = $mediaData["total"];
                $medias = $mediaTotal > 1 ? $mediaData["multimedia"]["multimedia"] : $mediaData["multimedia"];
                $theme["multimedia_Data"] = $medias;
            }
        }
        unset($theme);
        if ($total > 0) {
            $paginator = Zend_Paginator::factory(1 * $total);
            $paginator->setItemCountPerPage($pagesize);
            $paginator->setCurrentPageNumber($page);
            $this->view->paginator = $paginator;
            $this->view->themes = $themes;
            $this->view->total = $total;
        }
        $viewStore=new Application_Model_ViewStore("NavigationManager", "getthemes",null,$page);
        $viewNs=new Zend_Session_Namespace("viewNs");
        $viewNs->levelOneView=$viewStore;
    }
}





